﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using ZLCMS.Data.Common;
using NHibernate.Criterion;
using NHibernate;
using DotNet.Common.NHibernateUtil;


namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class OrderView : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected ShopOrderInfo _ordersDB;//当前订单信息
        protected ShopShippingInfo _shippingDB;//当前配送方式
        protected ShopPaymentInfo _paymentDB;//当前付费方式
        protected MemberInfo _ordersMemberDB;//当前订单购买会员信息
        protected string _owner;

        protected string _userAccount;
        protected decimal _hxffje
        {
            get
            {
                return _ordersDB.TotalPrice - _ordersDB.UseAdvancePayment;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            
            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        #region 初始化全局变量 + protected void InitializeGlobalValue()
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        protected void InitializeGlobalValue()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (!IsPostBack)
                    {
                        _id.Text = Request["uid"];
                        _freshtabid.Text = String.IsNullOrEmpty(Request["freshtabid"]) ? _framePage.NavTabId : Request["freshtabid"];
                        _owner = UserKit.GetUserAccountFailLogin();
                    }

                    _ordersDB = session.Get<ShopOrderInfo>(int.Parse(_id.Text));

                    if (_ordersDB == null)
                    {
                        throw new Exception("找不到对应信息.");
                    }

                    _shippingDB = session.Get<ShopShippingInfo>(int.Parse(_ordersDB.ShippingType));

                    if (_shippingDB == null)
                    {
                        _shippingDB = new ShopShippingInfo();
                    }

                    _paymentDB = session.Get<ShopPaymentInfo>(int.Parse(_ordersDB.PaymentType));

                    if (_paymentDB == null)
                    {
                        _paymentDB = new ShopPaymentInfo();
                    }

                    _ordersMemberDB = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("AccountId", _ordersDB.MemberAccount)).Add(Expression.Eq("OwnerAccount", _owner)).UniqueResult<MemberInfo>();

                    if (_ordersMemberDB == null)
                    {
                        _ordersMemberDB = new MemberInfo();
                    }

                    String hql = "from ShopOrderProduct o, ShopProductInfo p where p.ProductNo = o.ProductNo and o.OrderNo = '" + _ordersDB.OrderNo + "'";
                    IQuery query = session.CreateQuery(hql);
                    grid.DataSource = query.List();
                    grid.DataBind();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        } 
        #endregion

        #region 初始化页面内容 + protected void InitializeContent()
        /// <summary>
        /// 初始化页面内容
        /// </summary>
        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                //页面控件数据

                //ParamKit.Bind("FaHuoZhe", ddl_fh, null, null, null, "-1", "-1");
                //ddl_fh.SelectedValue = _ordersDB.Fh.HasValue ? _ordersDB.Fh.Value.ToString() : "-1";


            }
        } 
        #endregion

        #region 保存操作 + protected void Save()
        /// <summary>
        /// 保存操作
        /// </summary>
        protected void Save()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (!string.IsNullOrEmpty(tb_totalprice.Text))
                    {
                        _ordersDB.TotalPrice = decimal.Parse(tb_totalprice.Text);
                    }
                    _ordersDB.ManageHistory += string.Format("<br><font color=red>【{0}】修改，({1})</font><br>{2}", UserKit.GetUserAccount(), DateTime.Now, tb_glyfy.Text);

                    session.Update(_ordersDB);
                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = _freshtabid.Text
            });
        } 
        #endregion
    }
}
